home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapte10 / server.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  342 lines

  1.  
  2. #include <windows.h>  
  3. #include "server.h"  
  4. #include "time.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "DDE Server"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  107.                                  UINT     uFmt,    
  108.                                  HCONV    hconv,    
  109.                                  HSZ      hsz1,    
  110.                                  HSZ      hsz2,    
  111.                                  HDDEDATA hdata,    
  112.                                  DWORD    dwData1,
  113.                                  DWORD    dwData2 );
  114.  
  115.  
  116. DWORD ddeInst    = 0;
  117. HSZ   hszService = NULL;
  118. HSZ   hszTopic1  = NULL;
  119. HSZ   hszTopic2  = NULL;
  120. HSZ   hszTopic3  = NULL;
  121. HSZ   hszItem    = NULL;
  122.  
  123. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  124. {
  125.    switch( uMsg )
  126.    {
  127.       case WM_CREATE :
  128.               // Initialize DDEML.
  129.               //..................
  130.               if ( DdeInitialize( &ddeInst, DdemlCallback, 
  131.                    APPCLASS_STANDARD, 0 ) != DMLERR_NO_ERROR )
  132.               {
  133.                  MessageBox( hWnd, "Could not initialize DDEML.", 
  134.                              NULL, MB_OK | MB_ICONSTOP );
  135.                  return( -1 );
  136.               }
  137.  
  138.               // Allocate strings.
  139.               //..................
  140.               hszService = DdeCreateStringHandle( ddeInst,"Server",CP_WINANSI );
  141.               hszTopic1  = DdeCreateStringHandle( ddeInst,"Data",CP_WINANSI );
  142.               hszTopic2  = DdeCreateStringHandle( ddeInst,"Msg",CP_WINANSI ); 
  143.               hszTopic3  = DdeCreateStringHandle( ddeInst,"Time",CP_WINANSI );
  144.               hszItem    = DdeCreateStringHandle( ddeInst,"TimeItem",CP_WINANSI );
  145.  
  146.               DdeNameService( ddeInst, hszService, 0L, DNS_REGISTER );
  147.               break;
  148.  
  149.       case WM_COMMAND :
  150.               switch( LOWORD( wParam ) )
  151.               {
  152.                  case IDM_ENABLE :
  153.                         DdeEnableCallback( ddeInst, NULL, EC_ENABLEONE );
  154.                         break;
  155.  
  156.                  case IDM_DISABLE :
  157.                         DdeEnableCallback( ddeInst, NULL, EC_DISABLE );
  158.                         break;
  159.  
  160.                  case IDM_ABOUT :
  161.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  162.                         break;
  163.  
  164.                  case IDM_EXIT :
  165.                         DestroyWindow( hWnd );
  166.                         break;
  167.               }
  168.               break;
  169.       
  170.       case WM_DESTROY :
  171.               DdeNameService( ddeInst, hszService, 0L, DNS_UNREGISTER );
  172.  
  173.               DdeFreeStringHandle( ddeInst, hszService );
  174.               DdeFreeStringHandle( ddeInst, hszTopic1 );
  175.               DdeFreeStringHandle( ddeInst, hszTopic2 );
  176.               DdeFreeStringHandle( ddeInst, hszTopic3 );
  177.               DdeFreeStringHandle( ddeInst, hszItem );
  178.  
  179.               DdeUninitialize( ddeInst );
  180.               PostQuitMessage(0);
  181.               break;
  182.  
  183.       default :
  184.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  185.    }
  186.  
  187.    return( 0L );
  188. }
  189.  
  190.  
  191. VOID CALLBACK TimeProc( HWND  hwnd, 
  192.                         UINT  uMsg,
  193.                         UINT  idEvent,
  194.                         DWORD dwTime )
  195. {
  196.    DdePostAdvise( ddeInst, hszTopic3, hszItem );
  197. }
  198.  
  199.  
  200. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  201.                                  UINT     uFmt,    
  202.                                  HCONV    hconv,    
  203.                                  HSZ      hsz1,    
  204.                                  HSZ      hsz2,    
  205.                                  HDDEDATA hdata,    
  206.                                  DWORD    dwData1,
  207.                                  DWORD    dwData2 )
  208. {
  209. static LPBYTE lpMsg    = NULL;
  210. static UINT   uTimerID = 0;
  211.  
  212.    switch( uType )
  213.    {
  214.       case XTYP_CONNECT :
  215.             if ( DdeCmpStringHandles( hsz2, hszService ) ) 
  216.                break;
  217.  
  218.             if ( DdeCmpStringHandles( hsz1, hszTopic1 ) &&
  219.                  DdeCmpStringHandles( hsz1, hszTopic2 ) &&
  220.                  DdeCmpStringHandles( hsz1, hszTopic3 ) )
  221.             {
  222.                break;
  223.             }
  224.  
  225.             // Allow the conversation.
  226.             //........................
  227.             return( (HDDEDATA)TRUE );
  228.  
  229.       case XTYP_REQUEST :
  230.             if ( !DdeCmpStringHandles( hsz1, hszTopic1 ) )
  231.             {
  232.                HDDEDATA hData = DdeCreateDataHandle( ddeInst, NULL, 50, 0, 
  233.                                                      hszTopic2, CF_TEXT, 0 );
  234.                if ( hData )
  235.                {
  236.                   SHORT i, offs;
  237.                   char  szTmp[3];
  238.  
  239.                   hData = DdeAddData( hData, "Data: ", 6, 0 );
  240.                   offs = 6;
  241.  
  242.                   for( i=0; i<10; i++ )
  243.                   {
  244.                      itoa( i, szTmp, 10 );
  245.                      hData = DdeAddData( hData, szTmp, strlen( szTmp ), offs );
  246.                      offs += strlen( szTmp );
  247.                   }
  248.                   szTmp[0] = 0;
  249.                   hData = DdeAddData( hData, szTmp, 1, offs );
  250.  
  251.                   return( hData );
  252.                }
  253.                return( NULL );
  254.             }
  255.             return( NULL );
  256.  
  257.              
  258.       case XTYP_EXECUTE :
  259.             if ( lpMsg )
  260.                return( (HDDEDATA)DDE_FBUSY );
  261.  
  262.             if ( !DdeCmpStringHandles( hsz1, hszTopic2 ) )
  263.             {
  264.                lpMsg = DdeAccessData( hdata, NULL );
  265.  
  266.                MessageBox( NULL, lpMsg, "DDE Message", MB_OK );
  267.  
  268.                DdeUnaccessData( hdata );
  269.                lpMsg = NULL;
  270.  
  271.                return( (HDDEDATA)DDE_FACK );
  272.             }
  273.             
  274.             return( (HDDEDATA)DDE_FNOTPROCESSED );
  275.       
  276.  
  277.       case XTYP_ADVSTART :
  278.             if ( DdeCmpStringHandles( hsz1, hszTopic3 ) == 0 &&
  279.                  DdeCmpStringHandles( hsz2, hszItem   ) == 0 )
  280.             {
  281.                uTimerID = SetTimer( NULL, 0, 1000, (TIMERPROC)TimeProc ); 
  282.                return( (HDDEDATA)TRUE );
  283.             }
  284.  
  285.             return( FALSE );
  286.  
  287.       case XTYP_ADVREQ :
  288.             if ( DdeCmpStringHandles( hsz1, hszTopic3 ) == 0 &&
  289.                  DdeCmpStringHandles( hsz2, hszItem   ) == 0 )
  290.             {
  291.                time_t   ltime;
  292.                char     szBuf[128];
  293.                HDDEDATA hDdeData;
  294.  
  295.                HSZ    hszDataName = DdeCreateStringHandle( ddeInst, 
  296.                                             "Hour", CP_WINANSI );
  297.                time( <ime );
  298.                wsprintf( szBuf, "The time is now: %s ", 
  299.                                  (LPTSTR)ctime(<ime) );
  300.  
  301.                hDdeData = DdeCreateDataHandle( ddeInst, szBuf,
  302.                                      strlen( szBuf )+1, 0,
  303.                                      hszDataName, CF_TEXT, 0 ); 
  304.  
  305.                DdeFreeStringHandle( ddeInst, hszDataName );
  306.                return( hDdeData );
  307.             }
  308.             return( NULL );
  309.  
  310.       case XTYP_ADVSTOP :
  311.             KillTimer( NULL, uTimerID );
  312.             break;
  313.  
  314.    }
  315.  
  316.    return( NULL );
  317. }
  318.  
  319.  
  320. LRESULT CALLBACK About( HWND hDlg,           
  321.                         UINT message,        
  322.                         WPARAM wParam,       
  323.                         LPARAM lParam)
  324. {
  325.    switch (message) 
  326.    {
  327.        case WM_INITDIALOG: 
  328.                return (TRUE);
  329.  
  330.        case WM_COMMAND:                              
  331.                if (   LOWORD(wParam) == IDOK         
  332.                    || LOWORD(wParam) == IDCANCEL)    
  333.                {
  334.                        EndDialog(hDlg, TRUE);        
  335.                        return (TRUE);
  336.                }
  337.                break;
  338.    }
  339.  
  340.    return (FALSE); 
  341. }
  342.